package org.gulugulubiao.controller;

import lombok.extern.slf4j.Slf4j;
import org.gulugulubiao.dto.ProblemPageQueryDTO;
import org.gulugulubiao.entity.InputAnswerEntity;
import org.gulugulubiao.entity.JudgeResult;
import org.gulugulubiao.entity.Problem;
import org.gulugulubiao.result.PageResult;
import org.gulugulubiao.result.Result;
import org.gulugulubiao.service.ProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/problem")
@Slf4j
public class ProblemController {

    @Autowired
    private ProblemService problemService;

    /**
     * 判断提交的答案
     */
    @PostMapping("/judge/{problemId}")
    public Result problemJudge(@PathVariable Integer problemId, @RequestBody InputAnswerEntity inputAnswerEntity) {
        log.info("题目id:{}", problemId);
        log.info("题目inputAnswer:{}", inputAnswerEntity.getInputAnswer());
        //提交答案
        JudgeResult judgeResult = problemService.problemJudge(problemId, inputAnswerEntity.getInputAnswer());
        return Result.success(judgeResult);
    }

    @PostMapping("/add")
    public Result addProblem(@RequestBody Problem problem) {

        log.info("新增题目:{}", problem);
        problemService.add(problem);
        return Result.success();

    }

    /**
     * 显示id对应的题目
     *
     * @param problemId
     * @return
     */
    @GetMapping("/{problemId}")
    public Result getProblem(@PathVariable Integer problemId) {
        log.info("获取题目id:{}", problemId);
        Problem problem = problemService.getProblem(problemId);
        return Result.success(problem);
    }

    /**
     * 删除id对应的题目
     *
     * @param problemId
     * @return
     */
    @DeleteMapping("/{problemId}")
    public Result deleteProblem(@PathVariable Integer problemId) {
        log.info("删除题目id:{}", problemId);
        problemService.deleteProblem(problemId);
        return Result.success();
    }

    /**
     * 查询题目列表
     */
//    @GetMapping("/list")
//    public Result getAllProblem() {
//        log.info("获取所有题目列表");
//
//        return problemService.getAllProblem();
//    }


    /**
     * 题目分页查询
     * @param problemPageQueryDTO
     * @return
     */
    @GetMapping("/list")
    public Result<PageResult> getProblemList(ProblemPageQueryDTO problemPageQueryDTO) {
        log.info("题目列表分页查询：{}", problemPageQueryDTO);
        PageResult result = problemService.getProblemList(problemPageQueryDTO);
        return Result.success(result);

    }

}
